package com.xudow.app.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.activeshare.edu.ucenter.common.messages.course.ListCourseMessage;
import com.activeshare.edu.ucenter.models.course.CourseWithOtherInfo;
import com.makeramen.roundedimageview.RoundedImageView;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.util.ImageUtils;
import com.xudow.app.util.LngLatUtil;
import com.xudow.app.util.StringUtils;

import java.util.List;

/**
 * 我要报名ListView Adapter
 *
 * @author lukecheng
 */
public class CourseAdapter extends UltimateViewAdapter<CourseAdapter.OrderViewHolder> {

    public interface OnRecyclerViewListener {
        void onItemClick(int position);

        boolean onItemLongClick(int position);
    }

    private OnRecyclerViewListener onRecyclerViewListener;

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }

    /**
     * 订单
     */
    private List<ListCourseMessage.CourseListEntity> courses;
    private Context context;

    public CourseAdapter(Context context, List<ListCourseMessage.CourseListEntity> courses) {
        this.context = context;
        this.courses = courses;
    }

    @Override
    public OrderViewHolder onCreateViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_course_list, null);

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);

        return new OrderViewHolder(view, true);
    }

    @Override
    public OrderViewHolder getViewHolder(View view) {
        return new OrderViewHolder(view, false);
    }

    @Override
    public void onBindViewHolder(OrderViewHolder holder, int position) {
        OrderViewHolder viewHolder = holder;

        if (null == viewHolder.courseName) {
            return;
        }

        ListCourseMessage.CourseListEntity course = courses.get(position);
        viewHolder.courseName.setText(course.getName());
        String agencyName="";
        if (null==course.getAgencyName()&&null!=course.getTeacher())
            agencyName=course.getTeacher();
        else
            agencyName=course.getAgencyName();
        viewHolder.agencyName.setText("(" + agencyName+ ")");
        /*if (course.getType()==1)
            viewHolder.courseType.setText("一对一");
        else if (course.getType()==2)
            viewHolder.courseType.setText("辅导班");*/
        viewHolder.courseType.setText(course.getTypeName());
        viewHolder.realPrice.setText("￥" + course.getCoursePrice());
        viewHolder.actualPrice.setText("￥" + course.getActualPrice());
        String address="";
        if (null!=course.getSchoolName())
            address=course.getSchoolName();

        viewHolder.address.setText(address);

        viewHolder.nianji.setText(course.getGradeName());
        String imageUrl = String.format(Config.IMAGE_LOAD_URL_PARAMS, course.getThumbnail());
        ImageUtils.loadImage(context, viewHolder.agencyImage, imageUrl, R.mipmap.course_emt_ico2);

        //计算距离
        XApplication application = (XApplication) context.getApplicationContext();
        if (null != application &&
                null != course.getLongtitude() && null != course.getLatitude() &&
                null != application.longitude && null != application.latitude) {
            String distance = LngLatUtil.distanceFormatted(course.getLongtitude().doubleValue(), course.getLatitude().doubleValue(),
                    application.longitude, application.latitude);

            if (!StringUtils.isEmpty(distance)) {
                viewHolder.distance.setText(distance);
            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup viewGroup) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder viewHolder, int i) {

    }

    @Override
    public int getItemCount() {
        return courses.size() + 1;
    }

    @Override
    public int getAdapterItemCount() {
        return courses.size() + 1;
    }

    @Override
    public long generateHeaderId(int i) {
        if (getItem(i).length() > 0)
            return getItem(i).charAt(0);
        else return -1;
    }

    public String getItem(int position) {
        if (customHeaderView != null) {
            position--;
        }
        if (position < courses.size()) {
            return courses.get(position).getName();
        } else {
            return "";
        }
    }

    class OrderViewHolder extends UltimateRecyclerviewViewHolder
            implements View.OnClickListener, View.OnLongClickListener {

        private RelativeLayout detailItem;
       // TODO 圆角图片 public ImageView agencyImage;
        public RoundedImageView agencyImage;
        public TextView courseName;
        public TextView courseType;
        public TextView address;
        public TextView distance;
        public TextView realPrice;
        public TextView actualPrice;
        public TextView agencyName;
        public TextView  nianji;
        public OrderViewHolder(View itemView, boolean isItem) {
            super(itemView);

            if (isItem) {
                detailItem = (RelativeLayout) itemView.findViewById(R.id.detail_item);
                detailItem.setOnClickListener(this);
                detailItem.setOnLongClickListener(this);
                //设置背景透明度
                itemView.findViewById(R.id.info_bg).getBackground().setAlpha(200);
                agencyImage = (RoundedImageView) itemView.findViewById(R.id.agency_img);
                agencyImage.setCornerRadius((float)5);
                courseName = (TextView) itemView.findViewById(R.id.course_name);
                courseType = (TextView) itemView.findViewById(R.id.course_type);
                address = (TextView) itemView.findViewById(R.id.address);
                distance = (TextView) itemView.findViewById(R.id.distance);
                realPrice = (TextView) itemView.findViewById(R.id.real_price);
                actualPrice = (TextView) itemView.findViewById(R.id.actual_price);
                agencyName = (TextView) itemView.findViewById(R.id.agency_name);

                nianji = (TextView) itemView.findViewById(R.id.nianji);
            }
        }

        @Override
        public void onClick(View v) {
            if (null != onRecyclerViewListener) {
                onRecyclerViewListener.onItemClick(this.getAdapterPosition());
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (null != onRecyclerViewListener) {
                return onRecyclerViewListener.onItemLongClick(this.getAdapterPosition());
            }

            return false;
        }
    }
}
